﻿using System;
using System.Web.Mvc;
using FlickrXplorer.Web.Models;
using FlickrXplorer.Web.ViewData;
using FlickrXplorer.Web.Utilities;

namespace FlickrXplorer.Web.Controllers
{
    public class CommentController : Controller
    {
        private readonly ICommentModel model;
        
        
        public CommentController()
        {
            model = new CommentModel();
        }

        public CommentController(ICommentModel model)
        {
            this.model = model;
        }

        [MvcAction, AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddComment (string text, string photoId)
        {
            try
            {
                model.AddCommment(text, photoId);
                return Content("Comment Added");
            }
            catch (Exception ex)
            {
                return View("Error", new ControllerException { ErrorUrl = HttpContext.Request.RawUrl, Message = ex.Message });
            }
        }


        [MvcAction, AcceptVerbs(HttpVerbs.Get)]
        public ActionResult List(string photoId, int ? index, int? page, bool ? partial)
        {
            int skip = index == null ? 0 : index.Value*page.Value;
            int take = page == null ? int.MaxValue : page.Value;


            PhotoCommentViewData comments = new PhotoCommentViewData();

            try
            {
                comments = model.GetComments(photoId, skip, take, false);
                return Json(comments);
            }
            catch (Exception ex)
            {
                
                return View("Error", new ControllerException { ErrorUrl = HttpContext.Request.RawUrl, Message = ex.Message});
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string commentId, string photoId)
        {
            model.DeleteComment(commentId, photoId);
            return Content("Comment Deleted!");
        }
    }
}
